﻿using System;
using System.Collections;
using System.Configuration;
using System.Data; 
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.JCI.Common.Utility;
using System.Text;
using Microsoft.JCI.Bll;
using System.Collections.Generic;
using Microsoft.JCI.Common.Entity; 

namespace Microsoft.JCI.Portal.DispatchManagement
{
    public partial class DispatchList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ControlExtension.BindEnum(ddlDispatchStatus, EnumeratorHelper.GetFieldTexts(typeof(Enumerator.FormStatus)));
                ddlDispatchStatus.Items.Insert(0, new ListItem("全部", ""));
            }
        }

        

        protected void radgridDispatch_ItemCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
        {
            DispatchBll bll = new DispatchBll();


            Guid DispatchFormId = new Guid(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["DispatchFormId"].ToString());
            //if (e.CommandName.ToLower() == "delete")
            //{
            //    try
            //    {

            //        bll.Delete<DisDispatchFormEntity>(" and DispatchFormId='" + DispatchFormId + "'");
            //    }
            //    catch
            //    {
            //        ClientScript.RegisterStartupScript(this.GetType(), "sdf", "alert('该派工单下有工作单，请先删除工作单!');", true);
            //    }
            //}
            //else if (e.CommandName.ToLower() == "nullify")
            //{
                DisDispatchFormEntity entity = bll.Get<DisDispatchFormEntity>(DispatchFormId);
                if (entity != null)
                {
                    entity.FormStatus = (int)Enumerator.FormStatus.Nullify;
                    bll.Update(entity);
                }
            //}

            BindGrid(true);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindGrid(true);
        }

        private void BindGrid(bool IsNeedBind)
        { 
            DispatchBll bll = new DispatchBll();
            DataTable dt = bll.QueryDispatchForm(GetQueryCondition());
            this.radgridDispatch.DataSource = dt;

            if(IsNeedBind)
            radgridDispatch.DataBind();
        }

        private List<SqlDbParameter> GetQueryCondition()
        {
            List<SqlDbParameter> listParameters = new List<SqlDbParameter>();
            SqlDbParameter Parameter = null;

            if (!string.IsNullOrEmpty(this.txtDispatchOrderID.Text.Trim()))
            {
                Parameter = new SqlDbParameter();
                Parameter.ColumnName = "FormCode";
                Parameter.ColumnType = DbType.String;
                Parameter.ParameterName = "@FormCode";
                Parameter.ParameterValue = this.txtDispatchOrderID.Text.Trim();
                Parameter.QualificationType = SqlDbParameter.QualificationSymbol.Like;
                listParameters.Add(Parameter);
            }

            if (this.ddlDispatchStatus.SelectedValue != "")
            {
                Parameter = new SqlDbParameter();
                Parameter.ColumnName = "FormStatus";
                Parameter.ColumnType = DbType.Int32;
                Parameter.ParameterName = "@FormStatus";
                Parameter.ParameterValue = Convert.ToInt32(ddlDispatchStatus.SelectedValue);
                Parameter.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
                listParameters.Add(Parameter);
            } 

            return listParameters;
        }

        protected void radgridDispatch_NeedDataSource(object source, Telerik.WebControls.GridNeedDataSourceEventArgs e)
        {
            BindGrid(false);
        }

        protected void radgridDispatch_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
        {
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Item || e.Item.ItemType == Telerik.WebControls.GridItemType.AlternatingItem)
            {
                DataRowView dv = e.Item.DataItem as DataRowView;
                if (dv != null)
                {
                    Literal ltlFormStatus = e.Item.FindControl("ltlFormStatus") as Literal;
                    ltlFormStatus.Text = EnumeratorHelper.GetFieldText((Enumerator.FormStatus)Convert.ToInt32(dv["FormStatus"].ToString()));
                }
            }
        }


         }
}
